﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace FrogEngine
{
	public class WireFrameRenderService : I3DRenderService
	{
		public WireFrameRenderService(IEngine engine, Effect effect)
		{
			this.engine = engine;
			this.graphicsDevice = engine.GraphicsDevice;
			this.effect = effect;
			subscribedEntities = new HashSet<IGameEntity>();
		}

		public void Render(GameTime gameTime)
		{
			// entities with user defined primitives
			var userDefinedEntities = subscribedEntities.Where(a => a is IUserDefinedPrimitives);
			
			// entities with associated models
			var entitiesWithModels = subscribedEntities.Where(a => a is IHasModel);

			foreach (var technique in effect.Techniques)
			{
				foreach (var pass in technique.Passes)
				{
					// set effect parameters
					pass.Apply();
					// draw
				}
			}
		}

		private IEngine engine;
		public IEngine Engine
		{
			get { return engine; }
		}

		private HashSet<IGameEntity> subscribedEntities;
		public ISet<IGameEntity> SubscribedEntities
		{
			get { return subscribedEntities; }
		}

		public bool Subscribe(IGameEntity entity)
		{
			subscribedEntities.Add(entity);
			return true;
		}

		public bool Unsubscribe(IGameEntity entity)
		{
			subscribedEntities.Remove(entity);
			return true;
		}

		private int serviceOrder = 0;
		public int ServiceOrder
		{
			get { return serviceOrder; }
			set { serviceOrder = value; }
		}

		private Effect effect;
		public Effect Effect
		{
			get { return effect; }
			set { effect = value; }
		}

		private GraphicsDevice graphicsDevice;
		public GraphicsDevice GraphicsDevice { get { return graphicsDevice; } }
	}
}
